package com.hk.newsservice.service.impl;

import com.github.pagehelper.PageHelper;
import com.hk.model.News;
import com.hk.model.NewsType;
import com.hk.newsservice.mapper.NewsMapper;
import com.hk.newsservice.service.NewsService;
import com.hk.newsservice.service.NewsTypeService;
import com.hk.utlis.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;

/**
 * @Author: zhe
 * @Date: 2021/6/16 10:34
 * @Statement: 新闻Service层接口实现类
 */
@Service
public class NewsServiceImpl implements NewsService {

    @Autowired
    private NewsMapper newsMapper;

    @Autowired
    private NewsTypeService newsTypeService;

    @Override
    public List<News> findAll(Integer page, Integer limit, News t) {
        //        获取分页后的新闻
        PageHelper.startPage(page, limit);
        List<News> newsList = newsMapper.findAll(t);
//        查询所有新闻类型,匹配对应新闻类型
        List<NewsType> newsTypeList = newsTypeService.findAll(1, 0, new NewsType());
//        遍历新闻
        for (News news : newsList) {
//            遍历类型
            for (NewsType newsType : newsTypeList) {
                if (news.getTypeId() == newsType.getId()) {
                    news.setNewsType(newsType.getName());
                    break;
                }
                if (newsType.getNewsTypeList().size() > 0) {
                    getMatching(newsType,news);
                    break;
                }
            }
            if (news.getNewsType() == null) {
                news.setNewsType("暂无类型");
            }
        }
        return newsList;
    }

    /**
     * 递归判断新闻属于那个类型
     * @param newsTypeList
     * @param news 存储匹配的节点名称
     */
    public void getMatching(NewsType newsTypeList,News news) {
//        判断是否具有子节点
        if (newsTypeList.getNewsTypeList().size() == 0) {
            return;
        }
        for (NewsType type : newsTypeList.getNewsTypeList()) {
            if (news.getTypeId() == type.getId()) {
                news.setNewsType(type.getName());
                break;
            }
//                未匹配到继续递归搜索
            getMatching(type,news);
        }
    }

    @Override
    public int count(Integer page, Integer limit, News t) {
        int count = newsMapper.count(t);
        return count;
    }

    @Override
    public News findById(Integer id) {
        News news = newsMapper.findById(id);
        return news;
    }

    @Override
    public List<News> findByIds(Integer id) {
        List<News> newsList = newsMapper.findByIds(id);
        return newsList;
    }

    @Override
    public List<News> findByHits(int i) {
        List<News> newsList = newsMapper.findByHits(i);
        List<News> newsList1 = new ArrayList<>(2);
        if (newsList.isEmpty()) {
            return newsList;
        }
        //        设定出现首页规则由浏览次数决定，随机取两个
        Set<Integer> set = new HashSet<>(2);
        while (set.size() < 2) {
            int random = (int)(Math.random() * newsList.size());
            set.add(random);
        }
//        赋值
        for (Integer integer : set) {
            newsList1.add(newsList.get(integer));
        }
        return newsList1;
    }

    @Override
    public int save(News t) {
        String dateTimeString = DateUtils.dateTimeString(new Date());
        t.setGmtCreate(dateTimeString);
        t.setGmtModified(dateTimeString);
        int save = newsMapper.save(t);
        if (save == 1) {
            System.out.println("添加成功");
        }
        return save;
    }

    @Override
    public int update(News t) {
        //        修改日期时间
        String dateTimeString = DateUtils.dateTimeString(new Date());
        t.setGmtModified(dateTimeString);
        int update = newsMapper.update(t);
        if (update == 1) {
            System.out.println("新闻信息修改成功");
        }
        return update;
    }

    @Override
    public int updateHits(News t) {
        //        修改日期时间
        String dateTimeString = DateUtils.dateTimeString(new Date());
        t.setGmtModified(dateTimeString);
        int update = newsMapper.updateHits(t);
        if (update == 1) {
            System.out.println("新闻浏览次数修改成功");
        }
        return update;
    }


    @Override
    public int delete(Integer id) {
        int delete = newsMapper.delete(id);
        if (delete == 1) {
            System.out.println("新闻信息删除成功");
        }
        return delete;
    }

}
